<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Model extends CI_Model
{
	function MY_Model()
	{
		parent::__construct();
	}
	
	function getRows($query, $multiRow = 1, $resultType = 'array')
	{
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, $query) or $this->show_custom_error($this->mycommon->selfURL(), $query, mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
		$data = array();
		$function = "mysqli_fetch_".$resultType;
		if($multiRow)
			while($row = $function($result))
			{
				$data[] = $row;
			}
		else $data = $function($result);
		
		mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
	
	function getRowsOnMultiQuery($query, $resultType = 'array')
	{
		$data = array();
		$function = "mysqli_fetch_".$resultType;
		
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, $query) or $this->show_custom_error($this->mycommon->selfURL(), $query, mysqli_error($iconn));
		$i = 0;
		while (($result = mysqli_store_result($iconn)) !== false)
		{
			$data[$i] = array();
			while($row = $function($result))
			{
				$data[$i][] = $row;
			}
			$i++;
			if(!mysqli_next_result($iconn)) break;
		}
		
		if($result !== false) mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
	
	function exeQuery($query, $error = 0)
	{
		$iconn = $this->db->conn_id;
        
        if($error==0)
        {
            mysqli_multi_query($iconn, $query) or $this->show_custom_error($this->mycommon->selfURL(), $query, mysqli_error($iconn));
        }
		else
        {
            mysqli_multi_query($iconn, $query);
		}
        
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
	}
	function show_custom_error($url_error, $store_error, $mess = '')
	{
        echo $mess;
        $url_error = $this->mycommon->getSaveSqlStr($url_error);
        $store_error = $this->mycommon->getSaveSqlStr($store_error);
        $content_error = $this->mycommon->getSaveSqlStr($mess);
        $store = trim($this->mycommon->getNameStore($store_error));
        $time = time();
        $this->exeQuery("CALL ww_add_error_logs('$url_error', '$store_error', '$content_error', '$store', '$time');", 1);
        
        // call error
        die();
	}
}